<?php

class GalleryController extends Controller
{
	public function actionGetGallerys()
	{
		if(isset($_GET['place_id'])) {
			$gallerys = Gallery::model()->findAllByAttributes(array('place_id'=> $_GET['place_id']));
			echo CJSON::encode($gallerys);
		}
	}
	
	public function actionReorderPhoto()
	{
		$transaction = Yii::app()->db->beginTransaction();
		try {
			if(!isset($_POST['sort'])) {
				throw new PlatwoException(9999, 'Need parameter sort');
			}
			if(gettype($_POST['sort']) != 'array') {
				throw new PlatwoException(9999, 'Parameter sort != array');
			}
			$first = Photo::model()->findByPk($_POST['sort'], array("order"=> "seq DESC"));
			if($first == null) {
				throw new PlatwoException(9999, 'first == null');
			}
			$sort = array();
			$firstSeq = $first->seq;
			$expression = '';
			$params = array();
			foreach($_POST['sort'] as $key => $value) {
				$sort[$key + $first->seq] = $value;
				$expression .= " WHEN ".$value." THEN :photo_id_{$value}";
				$params[":photo_id_{$value}"] = $key;
			}
			$expression .= ' END ';
			if(Photo::model()->updateByPk($_POST['sort'], array('seq'=> new CDbExpression("CASE photo_id".$expression)), "", $params) === false){
				throw new PlatwoException(9999, 'update failed.');
			}
			$transaction->commit();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	public function actionDelete()
	{
		$transaction = Yii::app()->db->begintransaction();
		try {
			if(!isset($_GET['gallery_id'])) {
				throw new PlatwoException(9999, 'Need parameter gallery_id');
			}
			$gallery = Gallery::model()->findByPk($_GET['gallery_id']);
			$group_id = $gallery->photo_group;
			if($gallery == null) {
				throw new PlatwoException(9999, 'Gallery not found.');
			}
			if(!$gallery->delete()) {
				throw new PlatwoException(9999, 'Can not delete gallery');
			}
			if(Photo::model()->deleteAllByAttributes(array('photo_group'=> $group_id)) === FALSE) {
				throw new PlatwoException(9999, 'Can not delete photos in gallery');
			}
			$transaction->commit();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	public function actionReorder()
	{
		$transaction = Yii::app()->db->beginTransaction();
		try {
			if(!isset($_POST['sort'])) {
				throw new PlatwoException(9999, 'Need parameter sort');
			}
			if(gettype($_POST['sort']) != 'array') {
				throw new PlatwoException(9999, 'Parameter sort != array');
			}
			$first = Gallery::model()->findByPk($_POST['sort'], array("order"=> "seq DESC"));
			if($first == null) {
				$firstSeq = 0;
			}
			else {
				$firstSeq = $first->seq + 1;
			}
			$sort = array();
			$expression = '';
			$params = array();
			foreach($_POST['sort'] as $key => $value) {
				$sort[$key + $firstSeq] = $value;
				$expression .= " WHEN ".$value." THEN :gallery_id_{$value}";
				$params[":gallery_id_{$value}"] = $key + $firstSeq;
			}
			$expression .= ' END ';
			if(Gallery::model()->updateByPk($_POST['sort'], array('seq'=> new CDbExpression("CASE gallery_id".$expression)), "", $params) === false){
				throw new PlatwoException(9999, 'update failed.');
			}
			$transaction->commit();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	public function actionEdit()
	{
		$transaction = Yii::app()->db->beginTransaction();
		try {
			if(!isset($_POST['gallery_id'], $_POST['title'])) {
				throw new PlatwoException(9999, "Need parameters gallery_id, title");
			}
			$gallery = Gallery::model()->findByPk($_POST['gallery_id']);
			if($gallery == null) {
				throw new PlatwoException(9999, "not found gallery");
			}
			$gallery->title = $_POST['title'];
			if(!$gallery->save()) {
				throw new PlatwoException(9999, "Can't save gallery.");
			}
			if(isset($_POST['photos'])) {
				$expression = '';
				$params = array();
				$photo_ids = array();
				foreach($_POST['photos'] as $key => $value) {
					$photo_ids[] = $value['photo_id'];
					$expression .= " WHEN ".$value['photo_id']." THEN :photo_id_{$value['photo_id']}";
					$params[":photo_id_{$value['photo_id']}"] = $value['caption'];
				}
				$expression .= ' END ';
				if(Photo::model()->updateByPk($photo_ids, array('caption'=> new CDbExpression("CASE photo_id".$expression)), "", $params) === false){
					throw new PlatwoException(9999, 'update failed.');
				}
			}
			$transaction->commit();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	public function actionGetPhotos()
	{
		try {
			if(!isset($_GET['gallery_id'])){ throw new PlatwoException(9999, 'Need property gallery_id'); }
			$album = Gallery::model()->findByPk($_GET['gallery_id']);
			if($album == null){ throw new PlatwoException(9999, 'Album not found'); }
			$photos = Photo::model()->findAllByAttributes(array('photo_group'=> $album->photo_group), array("order"=> "seq"));
			$resp = array();
			foreach($photos as $key => $photo) {
				$buffer = $photo->getAttributes();
				$likeGroup = LikeGroup::model()->findByPk($photo->like_id);
				$commentGroup = CommentGroup::model()->findByPk($photo->comment_group);
				$buffer['like_count'] = ($likeGroup == null) ? 0: $likeGroup->like_count;
				$buffer['comment_count'] = ($commentGroup == null) ? 0: $commentGroup->count;
				$resp[] = $buffer;
			}
			echo CJSON::encode($resp);
		} catch(PlatwoException $e) {
			echo $e->toJSON();
		}
	}
}